﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace VS2048
{
    public partial class FormMain : Form
    {
        private int _lci = 0;
        private int _uci = 0;
        private int _rci = 0;
        private int _bci = 0;

        public FormMain()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            hgControl.Values.Description = "";
            hgControl.Values.Image = null;
            hgControl.Values.Heading = "";

            btnReStart.Text = "";
            btnReStart.ToolTipBody = "新游戏";
            btnReStart.Image = new Bitmap(Application.StartupPath + @"\Resources\NewGame.png");

            lblLeftSum.Values.Image = new Bitmap(Application.StartupPath + @"\Resources\DirectionLeft.png");
            lblLeftSum.Text = "";
            lblUpSum.Values.Image = new Bitmap(Application.StartupPath + @"\Resources\DirectionUp.png");
            lblUpSum.Text = "";
            lblRightSum.Values.Image = new Bitmap(Application.StartupPath + @"\Resources\DirectionRight.png");
            lblRightSum.Text = "";
            lblBottomSum.Values.Image = new Bitmap(Application.StartupPath + @"\Resources\DirectionBottom.png");
            lblBottomSum.Text = "";

            lblTotalCellNum.Text = "";
            lblCurrCellNum.Text = "";

            playBox.OnGameSummaryInfoChanged -= new Action<GameSummaryInfo>(UpdateGameSumInfo);
            playBox.OnGameSummaryInfoChanged += new Action<GameSummaryInfo>(UpdateGameSumInfo);
        }

        private void btnReStart_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要重新开始游戏?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                playBox.ResetGame();

                lblLeftSum.Text = "";
                lblUpSum.Text = "";
                lblRightSum.Text = "";
                lblBottomSum.Text = "";

                lblTotalCellNum.Text = "";
                lblCurrCellNum.Text = "";

                _lci = 0; _rci = 0; _uci = 0; _bci = 0;
            }
        }

        private void playBox_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 37:
                    ++_lci;
                    break;
                case 38:
                    ++_uci;
                    break;
                case 39:
                    ++_rci;
                    break;
                case 40:
                    ++_bci;
                    break;
            }
            UpdateSumInfo();
        }

        private void UpdateSumInfo()
        {
            lblLeftSum.Text = string.Format("左移操作次数 -> {0}", _lci);
            lblUpSum.Text = string.Format("上移操作次数 -> {0}", _uci);
            lblRightSum.Text = string.Format("右移操作次数 -> {0}", _rci);
            lblBottomSum.Text = string.Format("下移操作次数 -> {0}", _bci);
        }

        private void UpdateGameSumInfo(GameSummaryInfo info)
        {
            lblCurrCellNum.Text = string.Format("当前Cell个数 -> {0}", info.CurrentCellCount);
            lblTotalCellNum.Text = string.Format("总共产生Cell个数 -> {0}", info.TotalCellCount);
            lblScoreVal.Text = info.Score.ToString();
        }
    }
}
